﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using ShopOnline.Controls;

namespace ShopOnline
{
    public partial class ManageRelatedLinks : AdminBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ((AdminMaster)this.Master).SetPageTitle("Manage Related Links");
                gridLinks.Rebind();
                ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "hideEditForm();", true);
            }
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            try
            {
                int LinkID = 0;
                if (hidID.Value == "-1")
                {
                    LinkID = 0;
                }
                else if (hidID.Value != "")
                {
                    LinkID = int.Parse(hidID.Value);
                }

                try
                {
                    ShopOnline.BLL.RelatedLinks url = new ShopOnline.BLL.RelatedLinks();
                    if (LinkID > 0)
                        url.LoadByPrimaryKey(LinkID);
                    else
                    {
                        url.AddNew();
                    }

                    url.s_Title = txtTitle.Text.Trim();
                    url.s_Description = txtDescription.Text.Trim();
                    url.s_URL = txtURL.Text.Trim();

                    url.Save();
                    ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('All For You Online', 'Save successfully!', 8000);", 1), true);
                    gridLinks.Rebind();

                }
                catch (Exception ex)
                {
                    ErrorLog.WriteToFile("ManageRelatedLinks.aspx.cs", "btnAddNew_OnClick", ex.StackTrace);
                }

                hidID.Value = "";
                ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "hideEditForm();", true);
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ManageRelatedLinks.aspx.cs", "btnSave_OnClick", ex.StackTrace);
                ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showEditForm();", true);
            }
        }

        protected void dataPager_OnPageIndexChanged(object sender, PageIndexChangedEventArgs e)
        {
            DataPagerCtrl pager = (DataPagerCtrl)sender;
            gridLinks.Rebind();
        }

        protected void gridLinks_OnNeedDataSource(object sender, EventArgs e)
        {
            ShopOnline.BLL.RelatedLinks links = new BLL.RelatedLinks();
            links.Search("", "[ID] desc", dataPager.CurrentPageIndex, dataPager.PageSize);
            gridLinks.DataSource = links.DefaultView;
        }

        protected void btnEdit_OnClick(object sender, EventArgs e)
        {
            LinkButton lnk = (LinkButton)sender;
            int id = int.Parse(lnk.Attributes["ItemID"].ToString());
            hidID.Value = id.ToString();
            ShopOnline.BLL.RelatedLinks links = new BLL.RelatedLinks();
            links.LoadByPrimaryKey(id);

            txtTitle.Text = links.s_Title;
            txtDescription.Text = links.s_Description;
            txtURL.Text = links.s_URL;
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showEditForm(1);", true);
        }

        protected void btnDelete_OnClick(object sender, EventArgs e)
        {
            try
            {
                if (hidID.Value == "-1")
                {
                    ShopOnline.BLL.RelatedLinks links = new BLL.RelatedLinks();
                    links.LoadAll();
                    links.DeleteAll();
                    links.Save();
                }
                else if (hidID.Value != "")
                {
                    ShopOnline.BLL.RelatedLinks links = new BLL.RelatedLinks();
                    links.LoadByPrimaryKey(int.Parse(hidID.Value));
                    links.DeleteAll();
                    links.Save();
                }

                hidID.Value = "";
                gridLinks.Rebind();
                ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "hideEditForm();", true);
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("ManagePageBanners.aspx.cs", "btnDelete_OnClick", ex.StackTrace);
            }
        }
    }
}